#!/usr/bin/env python3
"""
检查页面源码中是否有完整的Key Features JSON数据
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
import time
import json
import re

def extract_json_data():
    """从页面源码中提取JSON数据"""
    
    chrome_options = Options()
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--window-size=1920,1080')
    # chrome_options.add_argument('--headless')
    
    service = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=chrome_options)
    
    try:
        print("🔍 从页面源码中提取Key Features JSON数据...")
        
        # 访问页面
        url = "https://www.ford.co.uk/shop/build-your-ford/ngc2/ice2/model?catalogId=WAEGB-CGE-2022-FocusMCAGBR202450&series=ACMAA_VS-DH&planId=c"
        driver.get(url)
        
        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, 'div[data-cy][class*="ProductTile"]'))
        )
        
        time.sleep(5)  # 等待所有数据加载
        
        # 获取页面源码
        page_source = driver.page_source
        print(f"✅ 获取页面源码，长度: {len(page_source)} 字符")
        
        # 查找JSON数据模式
        json_patterns = [
            r'window\.__INITIAL_STATE__\s*=\s*({.*?});',
            r'window\.__PRELOADED_STATE__\s*=\s*({.*?});',
            r'window\.__APOLLO_STATE__\s*=\s*({.*?});',
            r'__NEXT_DATA__["\']?\s*:\s*({.*?})',
            r'window\.__APP_DATA__\s*=\s*({.*?});',
            r'"keyFeatures"\s*:\s*\[(.*?)\]',
            r'"features"\s*:\s*\[(.*?)\]',
            r'"specifications"\s*:\s*{(.*?)}',
        ]
        
        found_json_data = []
        
        for i, pattern in enumerate(json_patterns):
            print(f"\n🔍 搜索模式 {i+1}: {pattern[:50]}...")
            
            matches = re.findall(pattern, page_source, re.DOTALL | re.IGNORECASE)
            
            if matches:
                print(f"✅ 找到 {len(matches)} 个匹配")
                
                for j, match in enumerate(matches[:3]):  # 只显示前3个匹配
                    print(f"  匹配 {j+1}: {match[:200]}...")
                    
                    # 尝试解析JSON
                    try:
                        if match.strip().startswith('{'):
                            data = json.loads(match)
                            found_json_data.append({
                                'pattern': pattern,
                                'match_index': j,
                                'data': data
                            })
                            print(f"    ✅ JSON解析成功")
                        else:
                            print(f"    ⚠️  不是完整的JSON对象")
                    except Exception as e:
                        print(f"    ❌ JSON解析失败: {e}")
            else:
                print(f"❌ 未找到匹配")
        
        # 专门搜索Key Features相关的文本
        print(f"\n🔍 搜索Key Features相关文本...")
        
        # 您提供的实际Key Features
        expected_features = [
            "Driver Assistance Technologies Including Front & Rear Parking Sensors And Cruise Control",
            "LED Rear Lights",
            "Electrically Operated, Heated, Power Fold Door Mirrors",
            "Grille With Chrome Surround And Horizontal Bars",
            "Dual-Zone Electronic Air Temperature Control",
            "Premium Sensico® Interior With Metal Grey Stitching",
            "Manual 4-Way Driver Seat Adjust, With Manual Lumbar Adjust",
            "Front And Rear Parking Sensors",
            "Lane-Keeping Aid With Lane-Keeping Assist",
            "Pre-Collision Assist With Autonomous Emergency Braking",
            "4.2\" Colour TFT Ice Blue Illumination Analogue Cluster",
            "Thatcham Alarm With Power Door Dead Locks"
        ]
        
        found_features = []
        for feature in expected_features:
            # 搜索完整特性
            if feature in page_source:
                found_features.append(feature)
                print(f"✅ 找到完整特性: {feature}")
            else:
                # 搜索部分特性（去掉一些词）
                short_feature = feature.split(',')[0].split(' Including')[0].split(' With')[0]
                if short_feature in page_source and len(short_feature) > 10:
                    found_features.append(f"{short_feature} (部分匹配)")
                    print(f"⚠️  找到部分特性: {short_feature}")
                else:
                    print(f"❌ 未找到: {feature}")
        
        print(f"\n📊 Key Features搜索结果:")
        print(f"  期望找到: {len(expected_features)} 个特性")
        print(f"  实际找到: {len(found_features)} 个特性")
        print(f"  缺失比例: {(len(expected_features) - len(found_features)) / len(expected_features) * 100:.1f}%")
        
        # 如果在源码中找到了缺失的特性，说明数据存在但没有在UI中显示
        if len(found_features) > 6:
            print("🎉 在页面源码中找到了更多的Key Features！")
            print("这意味着数据存在，但没有在当前的UI交互中显示")
        
        # 保存结果
        result = {
            'page_url': url,
            'page_source_length': len(page_source),
            'json_data_found': len(found_json_data),
            'expected_features_count': len(expected_features),
            'found_features_count': len(found_features),
            'found_features': found_features,
            'json_extracts': found_json_data[:2] if found_json_data else []  # 只保存前2个避免文件过大
        }
        
        with open('./release/json_extraction_result.json', 'w', encoding='utf-8') as f:
            json.dump(result, f, ensure_ascii=False, indent=2)
        
        print(f"\n💾 提取结果已保存到 ./release/json_extraction_result.json")
        
        # 如果找到了JSON数据，进一步分析
        if found_json_data:
            print(f"\n🔍 分析找到的JSON数据...")
            for i, json_info in enumerate(found_json_data[:2]):
                print(f"\nJSON数据 {i+1}:")
                data = json_info['data']
                
                # 递归搜索Key Features相关的数据
                def search_in_json(obj, path=""):
                    results = []
                    if isinstance(obj, dict):
                        for key, value in obj.items():
                            if 'feature' in key.lower() or 'specification' in key.lower():
                                results.append(f"{path}.{key}: {str(value)[:100]}")
                            results.extend(search_in_json(value, f"{path}.{key}"))
                    elif isinstance(obj, list):
                        for idx, item in enumerate(obj):
                            results.extend(search_in_json(item, f"{path}[{idx}]"))
                    return results
                
                feature_paths = search_in_json(data)
                if feature_paths:
                    print(f"  找到 {len(feature_paths)} 个可能的特性路径:")
                    for path in feature_paths[:5]:
                        print(f"    {path}")
        
        return result
        
    except Exception as e:
        print(f"❌ 提取过程中出现异常: {e}")
        import traceback
        traceback.print_exc()
        
    finally:
        print("🧹 清理资源...")
        driver.quit()

if __name__ == "__main__":
    extract_json_data()
